// 全局混入文件 residenceTime.js   计算每个页面的停留时长
const pageDurationTracker = {
	data() {
		return {
			pageEnterTime: 0, // 当前页面的进入时间戳
			currentRoutePath: '' // 当前页面路径标识
		}
	},
	onShow() {
		this.currentRoutePath = this.$scope.route; // 获取页面路径‌:ml-citation{ref="1" data="citationList"}
		this.pageEnterTime = Date.now(); // 记录进入时间‌:ml-citation{ref="1,5" data="citationList"}
	},
	onHide() {
		this._calculateDuration(); // 页面隐藏时立即计算时长‌:ml-citation{ref="1,8" data="citationList"}
	},
	onUnload() {
		this._calculateDuration(); // 页面销毁时补充计算‌:ml-citation{ref="1,6" data="citationList"}
	},
	methods: {
		_calculateDuration() {
			const stayTime = Date.now() - this.pageEnterTime;
			if (stayTime > 0) {
				// console.log(this.currentRoutePath);  // 页面路径
				// console.log(stayTime / 1000);  // 停留时间：多少秒
				// uni.request({
				// 	url: '/api/page_duration',
				// 	data: {
				// 		path: this.currentRoutePath,
				// 		duration: stayTime
				// 	}
				// }); // 按页面路径上报数据‌:ml-citation{ref="5,8" data="citationList"}
			}
		}
	}
}

export default pageDurationTracker;